/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simulate;

import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.File;

/**
 *
 * @author thang
 */
public class simulate {
    /**
     * @param args the command line arguments
     */

    //window data, 1 window has 5 set of data
    //each data set has 3 type: acceX, acceY, tiltZ
    static int[][] windowData = new int[5][3];

    public static void addToWindow(int acceX, int acceY, int tiltZ){
        //move 4 rows
        for (int i = 0; i < (windowData.length - 1); i++){
            for (int j = 0; j<windowData[i].length; j++){
                windowData[i][j] = windowData[i + 1][j];
            }
        }
        windowData[windowData.length-1][0] = acceX;
        windowData[windowData.length-1][1] = acceY;
        windowData[windowData.length-1][2] = tiltZ;
    }

    public static int convertScale(double in){
        double c1 = (int)(in * 100);

        //cut too high value
        if (c1 > 50){c1 = 50;}
        if (c1 < -50){c1 = -50;}

        //convet to positive integer;
        int r = (int)c1 + 50;

        return r;
    }

    public static void main(String[] args) {
        // TODO code application logic here
        Scanner rawFile = null;
        String line = null;
        String[] dataSet = null;
        OntologyDB odb = new OntologyDB();

        File fileObject = new File("../../temp/raw_sensor_data.txt");
        System.out.println(fileObject.getPath());
        if (fileObject.exists()){
            System.out.println("raw sensor data file exist");
        } else {
            System.out.println("raw sensor data file not exist");
            //try{
            
            //    fileObject.createNewFile();
            //}catch(IOException e){
            //System.out.println("Problem opening files");System.exit(0);
            //}
        }

        
        try{
            rawFile = new Scanner(new FileInputStream("../../temp/raw_sensor_data.txt"));
        } catch(FileNotFoundException e){
            System.out.println("Problem opening files");
        }
        
        while(rawFile.hasNextLine()){
            line = rawFile.nextLine();
            dataSet = line.split("\\;");
            //use dataSet[6,8,16] <-> AcceX, AcceY, TiltZ
            addToWindow(convertScale(Double.parseDouble(dataSet[6])), convertScale(Double.parseDouble(dataSet[8])), convertScale(Double.parseDouble(dataSet[16])));
            odb.addData(windowData);
            try{
                Thread.currentThread().sleep(300);
            } catch(InterruptedException ie){
                System.out.println("Interrupted");
            }
            System.out.println("\n"+line);
        }

        rawFile.close();
    }

}
